package com.yztc.orangeentertainment.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;

import com.bumptech.glide.Glide;
import com.facebook.drawee.view.SimpleDraweeView;
import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.bean.channalbean.ListBean;
import com.yztc.orangeentertainment.ui.main.channalcontentfragment.DeleteActivity;
import com.yztc.orangeentertainment.utils.DividerItemDecoration;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/9/25.
 */
public class ChannelContentAdapter extends BaseRecyclerAdapter<ListBean> {
    private Context context;
    private View view;
    private boolean flag;
    private ListBean listBean1;
    private boolean f;

    public ChannelContentAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        if (viewType == 1) {
            if (!"".equals(listBean1.getArticle()) && listBean1.getArticle() != null) {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.channal_content_rvhead,
                        parent,
                        false);

                return new MyViewHolder(view);
            } else {
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.channal_content_rvitem1, parent,
                        false);

                return new MyViewHOlder1(view);
            }
        } else if (viewType == 3) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout
                            .channal_content_rvitem2, parent,
                    false);

            return new MYViewHolder2(view);
        } else if (viewType == 4) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout
                            .channal_content_rvitem4, parent,
                    false);

            return new MYViewHolder4(view);
        } else if (viewType == 5) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout
                            .channal_content_rvitem3, parent,
                    false);

            return new MYViewHolder3(view);
        } else if (viewType == 6) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout
                            .channalarticlecontent_itemimage, parent,
                    false);

            return new MYViewHolder5(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.channal_content_rvitem1, parent,
                    false);

            return new MyViewHOlder1(view);
        }


    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBind(final RecyclerView.ViewHolder viewHolder, int RealPosition, ListBean listBean) {
        listBean1 = listBean;
        if (viewHolder instanceof MyViewHOlder1) {
            ((MyViewHOlder1) viewHolder)
                    .ivChannalrvitem1photo.setColorFilter(0x40000000);
            Glide.with(context).load(listBean.getPic()).placeholder( R.mipmap.loading )
                    .crossFade().into(((MyViewHOlder1) viewHolder)
                    .ivChannalrvitem1photo);
            ((MyViewHOlder1) viewHolder).tvChannalrvitem1title.setText(listBean.getTitle());
            if (listBean.getCat() != null && !"".equals(listBean.getCat())) {
                ((MyViewHOlder1) viewHolder).dvChannalrvitem1icon.setColorFilter(0x40000000);
                ((MyViewHOlder1) viewHolder).dvChannalrvitem1icon.setImageURI(listBean.getCat()
                        .getIcon());
                ((MyViewHOlder1) viewHolder).tvChannalrvitem1type.setText(listBean
                        .getCat().getName());
            }
            ((MyViewHOlder1) viewHolder).tvChannalrvitem1scanner.setText(listBean
                    .getReadnum() + "");
            ((MyViewHOlder1) viewHolder).ivChannalrvitem1delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, DeleteActivity.class);
                    context.startActivity(intent);
                }
            });

        }
        if (viewHolder instanceof MyViewHolder) {
            ((MyViewHolder) viewHolder).dvImage.setColorFilter(0x40000000);
            Glide.with(context).load(listBean.getPic()).placeholder( R.mipmap.loading ).crossFade().into(((MyViewHolder) viewHolder).dvImage);
            ((MyViewHolder) viewHolder).tvChannalvpheadtitle.setText(listBean.getTitle());
            MyChannalContentHSAdapter adapter = new MyChannalContentHSAdapter(listBean.getArticle()
                    , context);
            Log.i("size", "--------------------" + listBean.getType());
            ((MyViewHolder) viewHolder).rvChannalvphead.setAdapter(adapter);


        }

        if (viewHolder instanceof MYViewHolder2) {
                ((MYViewHolder2) viewHolder).sbChannalrvitem2.setEnabled(true);
                ((MYViewHolder2) viewHolder).sbChannalrvitem2.setProgress(0);
                ((MYViewHolder2) viewHolder).ivChannalrvitem2.setColorFilter(0x40000000);
                ((MYViewHolder2) viewHolder).btnChannalrvitem2.setText("确定");
                ((MYViewHolder2) viewHolder).btnChannalrvitem2.setBackground(context.getDrawable(R
                        .drawable.circlebuttongray));
                Glide.with(context).load(listBean.getGrade().getVoteimg().getSrc()).crossFade().placeholder( R.mipmap.loading ).into(((MYViewHolder2)
                        viewHolder)
                        .ivChannalrvitem2);
                ((MYViewHolder2) viewHolder).tvChannalrvitem2title.setText(listBean.getTitle());
                ((MYViewHolder2) viewHolder).sbChannalrvitem2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                    @Override
                    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                        double a = (double) (((MYViewHolder2) viewHolder).sbChannalrvitem2
                                .getMeasuredWidth() - 180) / 100;
                        int width = (int) (48 + a * progress);
                        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(100, 100);
                        p.setMargins(width, 0, 0, 0);
                        f = true;
                        ((MYViewHolder2) viewHolder).tvChannalrvitem2program.setTextColor(Color
                                .RED);
                        ((MYViewHolder2) viewHolder).btnChannalrvitem2.setBackground(context
                                .getDrawable(R.drawable.circlebuttonyellow));
                        ((MYViewHolder2) viewHolder).tvChannalrvitem2program.setLayoutParams(p);
                        ((MYViewHolder2) viewHolder).tvChannalrvitem2program.setText("" + progress);
                    }

                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {

                    }

                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {

                    }
                });
                ((MYViewHolder2) viewHolder).btnChannalrvitem2.setOnClickListener(new View.OnClickListener() {
                    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                    @Override
                    public void onClick(View v) {
                        if (f == true) {
                            ((MYViewHolder2) viewHolder).sbChannalrvitem2.setEnabled(false);
                            ((MYViewHolder2) viewHolder).btnChannalrvitem2.setBackgroundColor(Color.TRANSPARENT);
                            ((MYViewHolder2) viewHolder).btnChannalrvitem2.setBackgroundColor(Color
                                    .TRANSPARENT);
                            ((MYViewHolder2) viewHolder).btnChannalrvitem2.setText("当前总的平均分数为：" + ((int)
                                    (Math.random()
                                            * 30 + 70)));
                            ((MYViewHolder2) viewHolder).btnChannalrvitem2.setTextColor(Color.BLACK);
                            f = false;
                            viewHolder.itemView.setFocusable(false);
                            ((MYViewHolder2) viewHolder).sbChannalrvitem2.setFocusable(false);
                        }
                    }
                });


        }
        if (viewHolder instanceof MYViewHolder3) {
            ((MYViewHolder3) viewHolder).ivSuoluotu.setColorFilter(0x40000000);
            Glide.with(context).load(listBean.getPic()).placeholder( R.mipmap.loading ).crossFade().into(((MYViewHolder3) viewHolder).ivSuoluotu);
            ((MYViewHolder3) viewHolder).vvChannalrvitem3.setVideoURI(Uri.parse(listBean.getSrc()));
            ((MYViewHolder3) viewHolder).ivChannalrvplay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((MYViewHolder3) viewHolder).ivSuoluotu.setVisibility(View.GONE);
                    //开始播放视频
                    if (flag == false) {
                        flag = true;
                        ((MYViewHolder3) viewHolder).vvChannalrvitem3.start();
                        ((MYViewHolder3) viewHolder).ivChannalrvplay.setImageResource(R.mipmap.ic_feed_pause);
                        ((MYViewHolder3) viewHolder).vvChannalrvitem3.setMediaController(new MediaController(context));

                    } else {
                        flag = false;
                        ((MYViewHolder3) viewHolder).ivChannalrvplay.setImageResource(R.mipmap
                                .ic_feed_play);
                        ((MYViewHolder3) viewHolder).vvChannalrvitem3.pause();
                        ((MYViewHolder3) viewHolder).vvChannalrvitem3.setMediaController(new MediaController(context));
                    }
                }
            });


            if (listBean.getCat() != null && !"".equals(listBean.getCat())) {
                ((MYViewHolder3) viewHolder).ivChannalrvitem3icon.setImageURI(listBean.getCat().getIcon());
                ((MYViewHolder3) viewHolder).tvChannalrvitem3icon.setText(listBean.getCat().getName());
            }
            ((MYViewHolder3) viewHolder).tvChannalrvitem3title.setText(listBean.getTitle());
            ((MYViewHolder3) viewHolder).tvChannalrvitem3scanner1.setText(listBean.getReadnum() + "");
        }
        if (viewHolder instanceof MYViewHolder4) {
            ((MYViewHolder4)
                    viewHolder)
                    .gvChannalrvitem4.setColorFilter(0x40000000);
            Glide.with(context).load(listBean.getGif().get(0).getPic()).crossFade().placeholder( R.mipmap
                    .loading ).into(((MYViewHolder4)
                    viewHolder)
                    .gvChannalrvitem4);
            ((MYViewHolder4) viewHolder).tvChannalrvitem4title.setText(listBean.getTitle());
            ((MYViewHolder4) viewHolder).tvChannalrvitem4name.setText(listBean.getAuthor().getName());

        }
        if (viewHolder instanceof MYViewHolder5) {
            ((MYViewHolder5) viewHolder).ivArticlecontentiv.setColorFilter(0x40000000);
            Glide.with(context).load(listBean.getOperation().getOp()).into(((MYViewHolder5) viewHolder).ivArticlecontentiv);
        }
    }

    class MyViewHolder extends Holder {
        @InjectView(R.id.dv_image)
        SimpleDraweeView dvImage;
        @InjectView(R.id.tv_channalvpheadtitle)
        TextView tvChannalvpheadtitle;
        @InjectView(R.id.tv_channalvpheadseemore)
        TextView tvChannalvpheadseemore;
        @InjectView(R.id.rv_channalvphead)
        RecyclerView rvChannalvphead;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, view);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            //水平摆放
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            //间距
            rvChannalvphead.addItemDecoration(new DividerItemDecoration(5));
            rvChannalvphead.setLayoutManager(linearLayoutManager);
        }
    }

    class MyViewHOlder1 extends Holder {
        @InjectView(R.id.tv_channalrvitem1title)
        TextView tvChannalrvitem1title;
        @InjectView(R.id.dv_channalrvitem1icon)
        SimpleDraweeView dvChannalrvitem1icon;
        @InjectView(R.id.tv_channalrvitem1type)
        TextView tvChannalrvitem1type;
        @InjectView(R.id.iv_channalrvitem1scanner)
        ImageView ivChannalrvitem1scanner;
        @InjectView(R.id.tv_channalrvitem1scanner)
        TextView tvChannalrvitem1scanner;
        @InjectView(R.id.iv_channalrvitem1delete)
        ImageView ivChannalrvitem1delete;
        @InjectView(R.id.iv_channalrvitem1photo)
        ImageView ivChannalrvitem1photo;

        public MyViewHOlder1(View itemView) {
            super(itemView);
            ButterKnife.inject(this, view);
            tvChannalrvitem1scanner = (TextView) itemView.findViewById(R.id.tv_channalrvitem1scanner);
        }
    }

    class MYViewHolder2 extends Holder {
        @InjectView(R.id.iv_channalrvitem2)
        ImageView ivChannalrvitem2;
        @InjectView(R.id.tv_channalrvitem2title)
        TextView tvChannalrvitem2title;
        @InjectView(R.id.tv_channalrvitem2program)
        TextView tvChannalrvitem2program;
        @InjectView(R.id.sb_channalrvitem2)
        SeekBar sbChannalrvitem2;
        @InjectView(R.id.btn_channalrvitem2)
        Button btnChannalrvitem2;

        public MYViewHolder2(View itemView) {
            super(itemView);
            ButterKnife.inject(this, view);
        }
    }

    class MYViewHolder3 extends Holder {
        @InjectView(R.id.vv_channalrvitem3)
        VideoView vvChannalrvitem3;
        @InjectView(R.id.iv_suoluotu)
        ImageView ivSuoluotu;
        @InjectView(R.id.iv_channalrvplay)
        ImageView ivChannalrvplay;
        @InjectView(R.id.tv_channalrvitem3title)
        TextView tvChannalrvitem3title;
        @InjectView(R.id.iv_channalrvitem3icon)
        SimpleDraweeView ivChannalrvitem3icon;
        @InjectView(R.id.tv_channalrvitem3icon)
        TextView tvChannalrvitem3icon;
        @InjectView(R.id.iv_channalrvitem3scanner)
        ImageView ivChannalrvitem3scanner;
        @InjectView(R.id.tv_channalrvitem3scanner1)
        TextView tvChannalrvitem3scanner1;
        @InjectView(R.id.iv_channalrvitem3delete)
        ImageView ivChannalrvitem3delete;

        public MYViewHolder3(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }

    class MYViewHolder4 extends Holder {
        @InjectView(R.id.gv_channalrvitem4)
        ImageView gvChannalrvitem4;
        @InjectView(R.id.tv_channalrvitem4title)
        TextView tvChannalrvitem4title;
        @InjectView(R.id.tv_channalrvitem4name)
        TextView tvChannalrvitem4name;
        @InjectView(R.id.iv_channalrvitem4share)
        Button ivChannalrvitem4share;

        public MYViewHolder4(View itemView) {
            super(itemView);
            ButterKnife.inject(this, view);
        }
    }

    class MYViewHolder5 extends Holder {
        @InjectView(R.id.iv_articlecontentiv)
        ImageView ivArticlecontentiv;
        public MYViewHolder5(View itemView) {
            super(itemView);
            ButterKnife.inject(this, view);
        }
    }

}
